package com.xqxyxchy.charts;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Vector;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.encoders.ImageFormat;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.PieLabelLinkStyle;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.chart.ui.RectangleInsets;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.data.category.DefaultCategoryDataset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xqxyxchy.charts.entity.Serie;

public class Charts {

    private static final Logger LOGGER = LoggerFactory.getLogger(Charts.class);

    /**
     * Jfreechart工具类
     * <p>
     * 解决中午乱码问题<br>
     * 用来创建类别图表数据集、创建饼图数据集、时间序列图数据集<br>
     * 用来对柱状图、折线图、饼图、堆积柱状图、时间序列图的样式进行渲染<br>
     * 设置X-Y坐标轴样式
     * <p>
     */
    private static final String NO_DATA_MSG = "数据加载失败";
    private static final Font FONT = new Font("宋体", Font.PLAIN, 22);

    private static final Color[] CHART_COLORS = {new Color(13, 131, 198), new Color(18, 76, 7),
        new Color(255, 117, 153), new Color(255, 204, 102), new Color(255, 188, 117), new Color(153, 158, 255),
        new Color(253, 236, 109), new Color(128, 133, 232), new Color(158, 90, 102), new Color(92, 92, 97)};// 颜色

    private static final List<String> CHART_TYPE = new ArrayList<String>();
    private String chartType;

    /**
     * 静态代码块
     */
    static {
        setChartTheme();
        CHART_TYPE.add("line");
        CHART_TYPE.add("bar");
        CHART_TYPE.add("stacked");
    }

    public Charts(String chartType) {
        this.chartType = chartType;
        if (!CHART_TYPE.contains(chartType)) {
            throw new RuntimeException("Type chart of " + chartType + " Not supported.");
        }
    }

    /**
     * 中文主题样式 解决乱码
     */
    private static void setChartTheme() {
        // 设置中文主题样式 解决乱码
        StandardChartTheme chartTheme = new StandardChartTheme("CN");
        // 设置标题字体
        chartTheme.setExtraLargeFont(FONT);
        // 设置图例的字体
        chartTheme.setRegularFont(FONT);
        // 设置轴向的字体
        chartTheme.setLargeFont(FONT);
        chartTheme.setSmallFont(FONT);
        chartTheme.setTitlePaint(new Color(51, 51, 51));
        chartTheme.setSubtitlePaint(new Color(85, 85, 85));

        chartTheme.setLegendBackgroundPaint(Color.WHITE);// 设置标注
        chartTheme.setLegendItemPaint(Color.BLACK);//
        chartTheme.setChartBackgroundPaint(Color.WHITE);

        Paint[] OUTLINE_PAINT_SEQUENCE = new Paint[] {Color.WHITE};
        // 绘制器颜色源
        DefaultDrawingSupplier drawingSupplier = new DefaultDrawingSupplier(CHART_COLORS, CHART_COLORS,
            OUTLINE_PAINT_SEQUENCE, DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE, DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE);
        chartTheme.setDrawingSupplier(drawingSupplier);

        chartTheme.setPlotBackgroundPaint(Color.WHITE);// 绘制区域
        chartTheme.setPlotOutlinePaint(Color.WHITE);// 绘制区域外边框
        chartTheme.setLabelLinkPaint(new Color(8, 55, 114));// 链接标签颜色
        chartTheme.setLabelLinkStyle(PieLabelLinkStyle.CUBIC_CURVE);

        chartTheme.setAxisOffset(new RectangleInsets(5, 12, 5, 12));
        chartTheme.setDomainGridlinePaint(new Color(192, 208, 224));// X坐标轴垂直网格颜色
        chartTheme.setRangeGridlinePaint(new Color(192, 192, 192));// Y坐标轴水平网格颜色

        chartTheme.setBaselinePaint(Color.WHITE);
        chartTheme.setCrosshairPaint(Color.BLUE);// 不确定含义
        chartTheme.setAxisLabelPaint(new Color(51, 51, 51));// 坐标轴标题文字颜色
        chartTheme.setTickLabelPaint(new Color(67, 67, 72));// 刻度数字
        chartTheme.setBarPainter(new StandardBarPainter());// 设置柱状图渲染
        chartTheme.setXYBarPainter(new StandardXYBarPainter());// XYBar 渲染

        chartTheme.setItemLabelPaint(Color.BLACK);
        chartTheme.setThermometerPaint(Color.WHITE);// 温度计

        ChartFactory.setChartTheme(chartTheme);
    }

    /**
     * 可以通过调用这个方法, 提供对应格式的参数即可生成图片,并存在指定位置 生成一个这先出并保存为png格式,
     *
     * @param title 图片标题
     * @param xtitle x轴标题
     * @param ytitle y轴标题
     * @param filepath 文件路径+文件名
     * @param categorie 横坐标类型
     * @param series 数据内容
     * @param width 图片宽度
     * @param height 图片高度
     * @throws Exception
     */
    public void createToFile(String title, String xtitle, String ytitle, String filepath, List<String> categorie,
        List<Serie> series, int width, int height, String format, float quality, boolean encodeAlpha, int compression)
        throws Exception {
        ChartPanel chartPanel = createChart(title, xtitle, ytitle, categorie, series);
        // 将图片保存为png格式
        saveAsFile(format, chartPanel.getChart(), filepath, width, height, quality, encodeAlpha, compression);
    }

    /**
     * 可以通过调用这个方法, 提供对应格式的参数即可生成图片,并存在指定位置 生成一个这先出并保存为png格式,
     *
     * @param title 图片标题
     * @param xtitle x轴标题
     * @param ytitle y轴标题
     * @param categorie 横坐标类型
     * @param series 数据内容
     * @param width 图片宽度
     * @param height 图片高度
     * @throws Exception
     */
    public String createToBase64(String title, String xtitle, String ytitle, List<String> categorie, List<Serie> series,
        int width, int height, String format, float quality, boolean encodeAlpha, int compression) throws Exception {
        ChartPanel chartPanel = createChart(title, xtitle, ytitle, categorie, series);
        // 将图片保存为png格式
        return saveAsBase64(format, chartPanel.getChart(), width, height, quality, encodeAlpha, compression);
    }

    /**
     * 创建类别数据集合
     */
    private DefaultCategoryDataset createDataset(List<Serie> series, String[] categories) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (Serie serie : series) {
            String name = serie.getName();
            Vector<Object> data = serie.getData();
            if (data != null && categories != null && data.size() == categories.length) {
                for (int index = 0; index < data.size(); index++) {
                    String value = data.get(index) == null ? "" : data.get(index).toString();
                    if (isPercent(value)) {
                        value = value.substring(0, value.length() - 1);
                    }
                    if (isNumber(value)) {
                        dataset.setValue(Double.parseDouble(value), name, categories[index]);
                    }
                }
            }
        }
        return dataset;
    }

    /**
     * 是不是一个%形式的百分比
     *
     * @param str
     * @return
     */
    private boolean isPercent(String str) {
        return str != null ? str.endsWith("%") && isNumber(str.substring(0, str.length() - 1)) : false;
    }

    /**
     * 是不是一个数字
     *
     * @param str
     * @return
     */
    private boolean isNumber(String str) {
        return str != null ? str.matches("^[-+]?(([0-9]+)((([.]{0})([0-9]*))|(([.]{1})([0-9]+))))$") : false;
    }

    /**
     * 设置类别图表(CategoryPlot) X坐标轴线条颜色和样式
     *
     * @param axis
     */
    private void setXAixs(CategoryPlot plot) {
        Color lineColor = new Color(31, 121, 170);
        plot.getDomainAxis().setAxisLinePaint(lineColor);// X坐标轴颜色
        plot.getDomainAxis().setTickMarkPaint(lineColor);// X坐标轴标记|竖线颜色
        plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45);
    }

    /**
     * 设置类别图表(CategoryPlot) Y坐标轴线条颜色和样式 同时防止数据无法显示
     *
     * @param axis
     */
    private void setYAixs(CategoryPlot plot) {
        Color lineColor = new Color(192, 208, 224);
        ValueAxis axis = plot.getRangeAxis();
        axis.setAxisLinePaint(lineColor);// Y坐标轴颜色
        axis.setTickMarkPaint(lineColor);// Y坐标轴标记|竖线颜色
        // 隐藏Y刻度
        axis.setAxisLineVisible(false);
        axis.setTickMarksVisible(false);
        // Y轴网格线条
        plot.setRangeGridlinePaint(new Color(192, 192, 192));
        plot.setRangeGridlineStroke(new BasicStroke(1));

        plot.getRangeAxis().setUpperMargin(0.5);// 设置顶部Y坐标轴间距,防止数据无法显示
        plot.getRangeAxis().setLowerMargin(0.5);// 设置底部Y坐标轴间距
    }

    /**
     * 必须设置文本抗锯齿
     */
    private void setAntiAlias(JFreeChart chart) {
        chart.setTextAntiAlias(false);
    }

    // -----------------------------------------------------------------------------------------------------------------

    /**
     * 创建图形
     *
     * @param title 折线图标题
     * @param xtitle x轴标题
     * @param ytitle y轴标题
     * @param categorie 横坐标类别
     * @param series 数据集
     * @return
     * @throws Exception
     */
    private ChartPanel createChart(String title, String xtitle, String ytitle, List<String> categorie,
        List<Serie> series) throws Exception {
        DefaultCategoryDataset dataSet = createDataset(series, categorie.toArray(new String[categorie.size()]));
        // 2：创建Chart[创建不同图形]
        JFreeChart chart = null;
        if ("line".equals(chartType)) {
            chart = ChartFactory.createLineChart(title, xtitle, ytitle, dataSet);
            // 3:对柱子进行渲染[[采用不同渲染]]
            setLineRender(chart.getCategoryPlot(), true, true);//
        } else if ("stacked".equals(chartType)) {
            chart = ChartFactory.createStackedBarChart(title, xtitle, ytitle, dataSet);
            // 3:对柱子进行渲染[[采用不同渲染]]
            setStackedBarRender(chart.getCategoryPlot(), true);//
        } else {
            chart = ChartFactory.createBarChart(title, xtitle, ytitle, dataSet);
            // 3:对柱子进行渲染[[采用不同渲染]]
            setBarRender(chart.getCategoryPlot(), true);//
        }

        // 4:设置抗锯齿，防止字体显示不清楚
        setAntiAlias(chart);// 抗锯齿
        // 5:对其他部分进行渲染
        setXAixs(chart.getCategoryPlot());// X坐标轴渲染
        setYAixs(chart.getCategoryPlot());// Y坐标轴渲染
        // 设置标注无边框
        chart.getLegend().setFrame(new BlockBorder(Color.WHITE));
        // 6:使用chartPanel接收
        ChartPanel chartPanel = new ChartPanel(chart);
        return chartPanel;
    }

    /**
     * 设置折线图样式
     *
     * @param plot
     * @param isShowDataLabels 是否显示数据标签
     */
    private void setLineRender(CategoryPlot plot, boolean isShowDataLabels, boolean isShapesVisible) {
        plot.setNoDataMessage(NO_DATA_MSG);
        plot.setInsets(new RectangleInsets(10, 10, 0, 10), false);
        LineAndShapeRenderer renderer = (LineAndShapeRenderer)plot.getRenderer();

        renderer.setDefaultStroke(new BasicStroke(6F));
        if (isShowDataLabels) {
            renderer.setDefaultItemLabelsVisible(true);
            renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator(
                StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING, NumberFormat.getInstance()));
            renderer.setDefaultPositiveItemLabelPosition(
                new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER));// 位置
        }
        renderer.setDefaultShapesVisible(isShapesVisible);// 数据点绘制形状
        renderer.setDrawOutlines(true);
        renderer.setUseFillPaint(true);
        plot.setRenderer(renderer);// 将修改后的属性值保存到图中
        setXAixs(plot);
        setYAixs(plot);
    }

    /**
     * 设置堆叠图样式
     *
     * @param plot
     * @param isShowDataLabels 是否显示数据标签
     */
    private void setStackedBarRender(CategoryPlot plot, boolean isShowDataLabels) {
        plot.setNoDataMessage(NO_DATA_MSG);
        plot.setInsets(new RectangleInsets(10, 10, 0, 10), false);
        StackedBarRenderer renderer = (StackedBarRenderer)plot.getRenderer();

        renderer.setDefaultStroke(new BasicStroke(6F));
        if (isShowDataLabels) {
            renderer.setIncludeBaseInRange(true);// 显示每个柱的数值，并修改该数值的字体属性
            renderer.setDefaultItemLabelsVisible(true);
            renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator(
                StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING, NumberFormat.getInstance()));
            renderer.setDefaultPositiveItemLabelPosition(
                new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.CENTER));// 位置
        }
        renderer.setItemMargin(0.5D);// 设置每个柱之间距离
        renderer.setDrawBarOutline(true);
        plot.setRenderer(renderer);// 将修改后的属性值保存到图中
        setXAixs(plot);
        setYAixs(plot);
    }

    /**
     * 设置柱状图样式
     *
     * @param plot
     * @param isShowDataLabels 是否显示数据标签
     */
    private void setBarRender(CategoryPlot plot, boolean isShowDataLabels) {
        plot.setNoDataMessage(NO_DATA_MSG);
        plot.setInsets(new RectangleInsets(10, 10, 0, 10), false);
        BarRenderer renderer = (BarRenderer)plot.getRenderer();

        renderer.setDefaultStroke(new BasicStroke(6F));
        if (isShowDataLabels) {
            renderer.setDefaultItemLabelsVisible(true);
            renderer.setIncludeBaseInRange(true);// 显示每个柱的数值，并修改该数值的字体属性
            renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator(
                StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING, NumberFormat.getInstance()));
            renderer.setDefaultPositiveItemLabelPosition(
                new ItemLabelPosition(ItemLabelAnchor.INSIDE12, TextAnchor.TOP_CENTER));// 位置
        }
        renderer.setItemMargin(0.5D);// 设置每个柱之间距离
        renderer.setDrawBarOutline(true);
        plot.setRenderer(renderer);// 将修改后的属性值保存到图中
        setXAixs(plot);
        setYAixs(plot);
    }

    /**
     * 将图表保存图片
     *
     * @param chart 折线图对象
     * @param outputPath 文件保存路径, 包含文件名
     * @param weight 宽
     * @param height 高
     * @throws Exception
     */
    private void saveAsFile(String format, JFreeChart chart, String outputPath, int weight, int height, float quality,
        boolean encodeAlpha, int compression) throws Exception {
        FileOutputStream out = null;
        File outFile = new File(outputPath);
        if (!outFile.getParentFile().exists()) {
            outFile.getParentFile().mkdirs();
        }
        out = new FileOutputStream(outputPath);

        if (ImageFormat.JPEG.equalsIgnoreCase(format)) {
            ChartUtils.writeChartAsJPEG(out, quality, chart, weight, height);
        } else {
            ChartUtils.writeChartAsPNG(out, chart, weight, height, encodeAlpha, compression);
        }

        out.flush();
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                LOGGER.error("{}", e.getMessage(), e);
            }
        }
    }

    /**
     * <pre>
     * 将图表保存为PNG图片并转成base64
     * <img src="data:image/png;base64,${img5}" width="1000px">
     * <img src="data:image/jpeg;base64,${img5}" width="1000px">
     * </pre>
     *
     * @param chart 折线图对象
     * @param weight 宽
     * @param height 高
     * @throws Exception
     */
    private String saveAsBase64(String format, JFreeChart chart, int weight, int height, float quality,
        boolean encodeAlpha, int compression) throws Exception {
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        if (ImageFormat.JPEG.equalsIgnoreCase(format)) {
            ChartUtils.writeChartAsJPEG(out, quality, chart, weight, height);
        } else {
            ChartUtils.writeChartAsPNG(out, chart, weight, height, encodeAlpha, compression);
        }

        out.flush();
        String base64Image = Base64.getEncoder().encodeToString(out.toByteArray());
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                LOGGER.error("{}", e.getMessage(), e);
            }
        }
        return base64Image;
    }

}
